/* ansiStdio.c - ANSI `stdio' documentation */

/* Copyright 1984-2002 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
01j,21jan02,jkf  SPR#72774, fread is checking for NULL on wrong arg
01i,12dec01,jkf  fixing SPR#72128, fread should check for NULL before bcopy.
01h,11nov01,jkf  SPR#70967, fread() returns wrong value when 3rd arg == 0
01g,17mar99,jdi  doc: updated w/ info about proj facility (SPR 25727).
01f,04feb99,dgp  document errno values
01e,10feb95,jdi  doc format tweak in fopen() and update for rhp changes in 01d.
01d,24jan95,rhp  doc: avoid 'L' in fprintf() and fscanf(), no long doubles 
                 in VxWorks (see SPR#3886)
01c,19jul94,dvs  doc tweak (SPR #2512)
01b,05mar93,jdi  documentation cleanup for 5.1.
01a,24oct92,smb  created.
*/

/*
DESCRIPTION
The header stdio.h declares three types, several macros, and many functions
for performing input and output.
.SS Types
The types declared are `size_t' and:
.iP `FILE' 12
object type capable of recording all the information needed to
control a stream, including its file position indicator, a pointer to its
associated buffer (if any), an error indicator that records whether a
read/write error has occurred, and an end-of-file indicator that records
whether the end of the file has been reached.
.iP `fpos_t'
object type capable of recording all the information needed to
specify uniquely every position within a file.
.SS Macros
The macros are NULL and:
.iP "_IOFBF, _IOLBF, _IONBF" 12 3
expand to integral constant expressions with distinct values, suitable
for use as the third argument to setvbuf().
.iP BUFSIZ
expands to an integral constant expression that is the size of the
buffer used by setbuf().
.iP EOF
expands to a negative integral constant expression that is returned by
several functions to indicate `end-of-file', that is, no more input from a
stream.
.iP FOPEN_MAX
expands to an integral constant expression that is the minimum number of
the files that the system guarantees can be open simultaneously.
.iP FILENAME_MAX
expands to an integral constant expression that is the size needed for an
array of `char' large enough to hold the longest file name string that
can be used.
.iP L_tmpnam
expands to an integral constant expression that is the size needed for
an array of `char' large enough to hold a temporary file name string
generated by tmpnam().
.iP "SEEK_CUR, SEEK_END, SEEK_SET"
expand to integral constant expressions with distinct values suitable
for use as the third argument to fseek().
.iP TMP_MAX
expands to an integral constant expression that is the minimum number of
file names generated by tmpnam() that will be unique.
.iP "`stderr', `stdin', `stdout'"
expressions of type "pointer to FILE" that point to the FILE objects
associated, respectively, with the standard error, input, and output streams.

STREAMS
Input and output, whether to or from physical devices such as terminals and
tape drives, or whether to or from files supported on structured storage
devices, are mapped into logical data streams, whose properties are more
uniform than their various inputs and outputs. Two forms of mapping are
supported: for text streams and for binary streams.

A text stream is an ordered sequence of characters composed into lines,
each line consisting of zero or more characters plus a terminating new-line
character.  Characters may have to be added, altered, or deleted
on input and output to conform to differing conventions for representing
text in the host environment.  Thus, there is no need for a one-to-one
correspondence between the characters in a stream and those in the external
representation.  Data read in from a text stream will necessarily compare
equal to the data that were earlier written out to that stream only if:
the data consists only of printable characters and the control characters
horizontal tab and new-line; no new-line character is immediately preceded
by space characters; and the last character is a new-line character.
Space characters are written out immediately before a new-line character
appears.

A binary stream is an ordered sequence of characters that can transparently
record internal data.  Data read in from a binary stream should compare
equal to the data that was earlier written out to that stream, under the
same implementation.  However, such a stream may have a
number of null characters appended to the end of the stream.
.SS "Environmental Limits"
VxWorks supports text files with lines containing at least 254 characters,
including the terminating new-line character.  The value of the macro
BUFSIZ is 1024.

FILES
A stream is associated with an external file (which may be a physical
device) by opening a file, which may involve creating a new file.
Creating an existing file causes its former contents to be discarded, if
necessary.  If a file can support positioning requests (such as a disk
file, as opposed to a terminal), then a file position indicator associated
with the stream is positioned at the start (character number zero) of the
file.  The file position indicator is maintained by subsequent reads,
writes, and positioning requests, to facilitate an orderly progression
through the file.  All input takes place as if characters were read by
successive calls to fgetc(); all output takes place as if characters were
written by successive calls to fputc().

Binary files are not truncated, except as defined in fopen() documentation.

When a stream is unbuffered, characters are intended to appear from the
source or at the destination as soon as possible.  Otherwise characters
may be accumulated and transmitted to or from the host environment as a
block.  When a stream is fully buffered, characters are intended to be
transmitted to or from the host environment as a block when the buffer is
filled.  When a stream is line buffered, characters are intended to be
transmitted to or from the host environment as a block when a new-line
character is encountered.  Furthermore, characters are intended to be
transmitted as a block to the host environment when a buffer is filled,
when input is requested on an unbuffered stream, or when input is requested on
a line-buffered stream that requires the transmission of characters from
the host environment.  VxWorks supports these characteristics via the
setbuf() and setvbuf() functions.

A file may be disassociated from a controlling stream by closing the file.
Output streams are flushed (any unwritten buffer contents are transmitted
to the host environment) before the stream is disassociated from the file.
The value of a pointer to a FILE object is indeterminate after the associated
file is closed (including the standard text streams).

The file may be subsequently reopened, by the same or another program
execution, and its contents reclaimed or modified (if it can be repositioned
at its start).

TASK TERMINATION
ANSI specifies that if the main function returns to its original caller or
if exit() is called, all open files are closed (and hence all output
streams are flushed) before program termination.  This does \f3not\fP
happen in VxWorks.  The exit() function does not close all files opened
for that task.  A file opened by one task may be used and closed by another.
Unlike in UNIX, when a VxWorks task exits, it is the responsibility of the
task to fclose() its file pointers, except `stdin', `stdout', and
`stderr'.  If a task is to be terminated asynchronously, use kill() and
arrange for a signal handler to clean up.

The address of the FILE object used to control a stream may be significant;
a copy of a FILE object may not necessarily serve in place of the original.

At program startup, three text streams are predefined and need not be opened
explicitly:  standard input (for reading conventional input), standard output
(for writing conventional output), and standard error (for writing diagnostic
output).  When opened, the standard error stream is not fully buffered; the
standard input and standard output streams are fully buffered if and only if
the stream can be determined not to refer to an interactive device.

Functions that open additional (non-temporary) files require a file name,
which is a string.  VxWorks allows the same file to be open multiple times
simultaneously.  It is up to the user to maintain synchronization between
different tasks accessing the same file.

FIOLIB
Several routines normally considered part of standard I/O -- printf(),
sprintf(), vprintf(), vsprintf(), and sscanf() -- are not implemented as part
of the buffered standard I/O library; they are
instead implemented in fioLib.  They do not use the standard I/O buffering
scheme.  They are self-contained, formatted, but unbuffered I/O
functions.  This allows a limited amount of formatted I/O to be achieved
without the overhead of the standard I/O library.

SEE ALSO:
fioLib,
.I "American National Standard for Information Systems -"
.I "Programming Language - C, ANSI X3.159-1989: Input/Output (stdio.h)

INTERNAL
This documentation module is built by appending the following files:

    clearerr.c
    fclose.c
    fdopen.c
    feof.c
    ferror.c
    fflush.c
    fgetc.c
    fgetpos.c
    fgets.c
    fileno.c
    fopen.c
    fprintf.c
    fputc.c
    fputs.c
    fread.c
    freopen.c
    fscanf.c
    fseek.c
    fsetpos.c
    ftell.c
    fwrite.c
    getc.c
    getchar.c
    gets.c
    getw.c
    perror.c
    putc.c
    putchar.c
    puts.c
    putw.c
    rewind.c
    scanf.c
    setbuf.c
    setbuffer.c
    setvbuf.c
    stdioLib.c
    stdioShow.c
    tmpfile.c
    tmpnam.c
    ungetc.c
    vfprintf.c
*/

/* clearerr.c - clear error file for stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/* 
modification history 
-------------------- 
01d,05mar93,jdi  documentation cleanup for 5.1.
01c,21sep92,smb  corrected first line of file.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/ 
 
/*
DESCRIPTION
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*	This product includes software developed by the University of
*	California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"

#undef	clearerr

/******************************************************************************
*
* clearerr - clear end-of-file and error flags for a stream (ANSI)
* 
* This routine clears the end-of-file and error flags for a specified stream.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS: N/A
*
* SEE ALSO: feof(), ferror()
*/

void clearerr
    (
    FILE * fp		/* stream to clear EOF and ERROR flags for */
    )
    {
    __sclearerr(fp);
    }
/* fclose.c - close file  stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  added OBJ_VERIFY and memory reclaimation.
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdlib.h, error.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "errno.h"
#include "stdlib.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fclose - close a stream (ANSI)
*
* This routine flushes a specified stream and closes the associated file.
* Any unwritten buffered data is delivered to the host environment to be
* written to the file; any unread buffered data is discarded.  The stream
* is disassociated from the file.  If the associated buffer was allocated
* automatically, it is deallocated.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* Zero if the stream is closed successfully, or EOF if errors occur.
*
* ERRNO: EBADF
*
* SEE ALSO: fflush()
*/

int fclose
    (
    FAST FILE * fp	/* stream to close */
    )
    {
    FAST int r;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    if (fp->_flags == 0)		/* not open! */
	{	
	errno = EBADF;
	return (EOF);
	}

    r = (fp->_flags & __SWR) ? (__sflush(fp)) : (0);

    if (__sclose (fp) < 0)
	r = EOF;

    if (fp->_flags & __SMBF)
	free ((char *)fp->_bf._base);

    if (HASUB(fp))			/* ungetc buffer? */
	FREEUB(fp);

    if (HASLB(fp))			/* fgetline buffer? */
	FREELB(fp);

    fp->_flags = 0;			/* release this FILE for reuse */
    stdioFpDestroy (fp);		/* destroy the file pointer */

    return (r);
    }
/* fdopen.c - open file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01d,07jul93,jmm  fixed fdopen to validate the fd (spr 2197)
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: sys/types.h, fcntl.h, unistd.h, stdio.h, errno.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "sys/types.h"
#include "fcntl.h"
#include "unistd.h"
#include "errno.h"
#include "objLib.h"
#include "iosLib.h"
#include "private/stdioP.h"


/*******************************************************************************
*
* fdopen - open a file specified by a file descriptor (POSIX)
*
* This routine opens the file specified by the file descriptor <fd> and
* associates a stream with it.
* The <mode> argument is used just as in the fopen() function.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS: A pointer to a stream, or a null pointer if an error occurs,
* with `errno' set to indicate the error.
*
* ERRNO: EINVAL
*
* SEE ALSO: fopen(), freopen(),
* .br
* .I "Information Technology - POSIX - Part 1:"
* .I "System API [C Language], IEEE Std 1003.1"
*/

FILE * fdopen
    (
    int		 fd,	/* file descriptor */
    const char * mode	/* mode to open with */
    )
    {
    FAST FILE *	fp;
    int		flags;
    int 	oflags;

    /* validate the file descriptor */

    if (iosFdValue (fd) == ERROR)
        return (NULL);
    
    if ((flags = __sflags(mode, &oflags)) == 0)
	    return (NULL);

    /* Make sure the mode the user wants is a subset of the actual mode. */

#if FALSE  /*  XXX */
    if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
	return (NULL);

    tmp = fdflags & O_ACCMODE;

    if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) 
	{
	errno = EINVAL;
	return (NULL);
	}
#endif

    if ((fp = stdioFpCreate()) == NULL)
	return (NULL);

    fp->_flags = flags;

    /*
     * If opened for appending, but underlying descriptor does not have
     * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
     * end before each write.
     */

    if ((oflags & O_APPEND) /* XXX && !(fdflags & O_APPEND) */ )
	fp->_flags |= __SAPP;

    fp->_file = fd;

    return (fp);
    }
/* feof.c - end of file indicator function. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01d,05mar93,jdi  documentation cleanup for 5.1.
01c,21sep92,smb  tweaks for mg
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION 
 * 
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

NOMANUAL
*/ 

#include "vxWorks.h"
#include "stdio.h"

#undef feof

/******************************************************************************
* 
* feof - test the end-of-file indicator for a stream (ANSI)
*
* This routine tests the end-of-file indicator for a specified stream. 
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* Non-zero if the end-of-file indicator is set for <fp>.
*
* SEE ALSO: clearerr()
*/

int feof
    (
    FILE * fp	/* stream to test */
    )
    {
    return (__sfeof(fp));
    }
/* ferror.c - file error indicator function for stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/
  
/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

NOMANUAL
*/ 

#include "vxWorks.h"
#include "stdio.h"

#undef ferror

/******************************************************************************
*
* ferror - test the error indicator for a file pointer (ANSI)
* 
* This routine tests the error indicator for the stream pointed to by <fp>.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: Non-zero if the error indicator is set for <fp>.
*
* SEE ALSO: clearerr()
*/

int ferror
    (
    FILE * fp	/* stream to test */
    )
    {
    return (__sferror(fp));
    }
/* fflush.c - flush file stdio.h */

/* Copyright 1992-1995 Wind River Systems, Inc. */
 
/*
modification history
--------------------
01e,20jan95,ism  changed FIOFLUSH to FIOSYNC
01d,03oct94,ism  added FIOFLUSH ioctl to __sflush() as per SPR#2548
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf Added OBJ_VERIFY
	    smb taken from UCB stdio
*/
 
/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, error.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "errno.h"
#include "objLib.h"
#include "private/stdioP.h"
#include "sys/ioctl.h"
#include "ioLib.h"

/******************************************************************************
*
* fflush - flush a stream (ANSI)
*
* This routine writes to the file any unwritten data for a specified output
* or update stream for which the most recent operation was not input; for an
* input stream the behavior is undefined.
*
* CAVEAT
* ANSI specifies that if <fp> is a null pointer, fflush() performs the
* flushing action on all streams for which the behavior is defined; however,
* this is not implemented in VxWorks.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: Zero, or EOF if a write error occurs.
*
* ERRNO: EBADF
*
* SEE ALSO: fclose()
*/

int fflush
    (
    FAST FILE * fp	/* stream to flush */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    /* ANSI specifies that fflush (NULL) will flush all task's file pointers.
     * The problem is that we don't keep such a list today so we return EOF
     * for now.
     */

    if ((fp == NULL) ||
        ((fp->_flags & __SWR) == 0))
	{
	errno = EBADF;
	return (EOF);
	}

    return (__sflush(fp));
    }

/******************************************************************************
*
* __sflush - flush stream pointed to by fp.
* 
* INCLUDE: stdio.h 
*
* RETURNS: ZERO or EOF
* NOMANUAL
*/

int __sflush
    (
    FAST FILE *fp
    )
    {
    FAST unsigned char *p;
    FAST int n;
    FAST int t;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    t = fp->_flags;

    if ((t & __SWR) == 0)
	return (0);

    if ((p = fp->_bf._base) == NULL)
	return (0);

    n = fp->_p - p;		/* write this much */

    /* Set these immediately to avoid problems with longjmp and to allow
     * exchange buffering (via setvbuf) in user write function.
     */

    fp->_p = p;
    fp->_w = (t & (__SLBF|__SNBF)) ? (0) : (fp->_bf._size);

    for (; n > 0; n -= t, p += t) 
	{
	t = __swrite (fp, (char *)p, n);
	if (t <= 0) 
	    {
	    fp->_flags |= __SERR;
	    return (EOF);
	    }
	}

	(void)ioctl(fileno(fp), FIOSYNC, 0);

    return (0);
    }
/* fgetc.c - get character from file -- stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/*
modification history
-------------------
01d,05mar93,jdi  documentation cleanup for 5.1.
01c,13oct92,jdi  mangen fixes.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/
  
/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "private/stdioP.h"

/******************************************************************************
*
* fgetc - return the next character from a stream (ANSI)
*
* This routine returns the next character (converted to an `int') from the
* specified stream, and advances the file position indicator for the stream.
*
* If the stream is at end-of-file, the end-of-file indicator for the stream is
* set; if a read error occurs, the error indicator is set.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The next character from the stream, or EOF if the stream is at end-of-file
* or a read error occurs.
*
* SEE ALSO: fgets(), getc()
*/

int fgetc
    (
    FILE *  fp	/* stream to read from */
    )
    {
    return (getc(fp));
    }
/* fgetpos.c - get position in file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  written.
*/
  
/*
DESCRIPTION

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "objLib.h"


/******************************************************************************
*
* fgetpos - store the current value of the file position indicator for a stream (ANSI)
*
* This routine stores the current value of the file position indicator for a
* specified stream <fp> in the object pointed to by <pos>.  The value stored
* contains unspecified information usable by fsetpos() for repositioning
* the stream to its position at the time fgetpos() was called.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* Zero, or non-zero if unsuccessful, with `errno' set to indicate the error.
*
* SEE ALSO: fsetpos()
*/

int fgetpos
    (
    FILE *	fp,	/* stream */
    fpos_t *	pos	/* where to store position */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    return (!((*pos = ftell (fp)) != (fpos_t) -1));
    }
/* fgets.c - Read lines of text from a stream. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  taken from UCB stdio
*/
 
/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fgets - read a specified number of characters from a stream (ANSI)
*
* This routine stores in the array <buf> up to <n>-1 characters from a
* specified stream.  No additional characters are read after a new-line or
* end-of-line.  A null character is written immediately after the last
* character read into the array.
*
* If end-of-file is encountered and no characters have been read, the
* contents of the array remain unchanged.  If a read error occurs, the array
* contents are indeterminate.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* A pointer to <buf>, or a null pointer if an error occurs or end-of-file is
* encountered and no characters have been read.
*
* SEE ALSO: fread(), fgetc()
*/

char * fgets
    (
    char *	buf,	/* where to store characters */
    FAST size_t n,	/* no. of bytes to read + 1 */
    FAST FILE *	fp	/* stream to read from */
    )
    {
    FAST size_t		len;
    FAST char *		s;
    FAST uchar_t *      p;
    FAST uchar_t *      t;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (NULL);

    if (n < 2)				/* sanity check */
	return (NULL);

    s = buf;
    n--;				/* leave space for NULL */

    do 
	{
	if ((len = fp->_r) <= 0)	/* If the buffer is empty, refill it */
	    {
	    if (__srefill(fp))		/* EOF/error: partial or no line */
		{
		if (s == buf)
		    return (NULL);
		break;
		}
	    len = fp->_r;
	    }

	p = fp->_p;

	/*
	 * Scan through at most n bytes of the current buffer,
	 * looking for '\n'.  If found, copy up to and including
	 * newline, and stop.  Otherwise, copy entire chunk
	 * and loop.
	 */

	if (len > n)
	    len = n;

	t = memchr ((void *)p, '\n', len);

	if (t != NULL) 
	    {
	    len = ++t - p;
	    fp->_r -= len;
	    fp->_p = t;
	    (void) bcopy ((void *)p, (void *)s, len);
	    s[len] = 0;

	    return (buf);
	    }

	fp->_r -= len;
	fp->_p += len;

	(void) bcopy ((void *)p, (void *)s, len);

	s += len;

	} while ((n -= len) != 0);

    *s = 0;

    return (buf);
    }
/* fileno.c - determine file number for stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  taken from UCB stdio
*/
 
/*
DESCRIPTION
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "objLib.h"
#include "private/stdioP.h"

#undef fileno

/******************************************************************************
*
* fileno - return the file descriptor for a stream (POSIX)
* 
* This routine returns the file descriptor associated with a specified
* stream.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The file descriptor, or -1 if an error occurs, with `errno' set to indicate
* the error.
*
* SEE ALSO:
* .br
* .I "Information Technology - POSIX - Part 1:"
* .I "System API [C Language], IEEE Std 1003.1"
*/

int fileno
    (
    FILE *	fp	/* stream */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    return (__sfileno(fp));
    }
/* fopen.c - open a file. stdio.h */

/* Copyright 1992-1995 Wind River Systems, Inc. */

/*
modification history
--------------------
01d,10feb95,jdi  doc format tweak.
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  modified to use stdioFpCreate and close memory leaks.
	    smb  taken from UCB stdio.
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, error.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/ 

#include "vxWorks.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "ioLib.h"
#include "fcntl.h"
#include "errno.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fopen - open a file specified by name (ANSI)
*
* This routine opens a file whose name is the string pointed to by <file>
* and associates a stream with it.
* The argument <mode> points to a string beginning with one of the following 
* sequences:
* .iP r "" 3
* open text file for reading
* .iP w
* truncate to zero length or create text file for writing
* .iP a
* append; open or create text file for writing at end-of-file
* .iP rb
* open binary file for reading
* .iP wb
* truncate to zero length or create binary file for writing
* .iP ab
* append; open or create binary file for writing at end-of-file
* .iP r+
* open text file for update (reading and writing)
* .iP w+
* truncate to zero length or create text file for update.
* .iP a+
* append; open or create text file for update, writing at end-of-file
* .iP "r+b / rb+"
* open binary file for update (reading and writing)
* .iP "w+b / wb+"
* truncate to zero length or create binary file for update
* .iP "a+b / ab+"
* append; open or create binary file for update, writing at end-of-file
* .LP
*
* Opening a file with read mode (`r' as the first character in the <mode>
* argument) fails if the file does not exist or cannot be read.
*
* Opening a file with append mode (`a' as the first character in the <mode>
* argument) causes all subsequent writes to the file to be forced to the
* then current end-of-file, regardless of intervening calls to fseek().  In
* some implementations, opening a binary file with append mode (`b' as the
* second or third character in the <mode> argument) may initially position
* the file position indicator for the stream beyond the last data written,
* because of null character padding.  In VxWorks, whether append mode is
* supported is device-specific.
*
* When a file is opened with update mode (`+' as the second or third
* character in the <mode> argument), both input and output may be performed
* on the associated stream.  However, output may not be directly followed by
* input without an intervening call to fflush() or to a file positioning
* function (fseek(), fsetpos(), or rewind()), and input may not be directly
* followed by output without an intervening call to a file positioning
* function, unless the input operation encounters end-of-file.  Opening (or
* creating) a text file with update mode may instead open (or create) a
* binary stream in some implementations.
*
* When opened, a stream is fully buffered if and only if it can be determined
* not to refer to an interactive device.  The error and end-of-file
* indicators for the stream are cleared.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* A pointer to the object controlling the stream, or a null pointer if the
* operation fails.
*
* SEE ALSO: fdopen(), freopen()
*/

FILE * fopen
    (
    const char *	file,	/* name of file */
    const char *	mode	/* mode */
    )
    {
    FAST FILE *	fp;
    FAST int	f;
    int		flags;
    int		oflags;

    if ((flags = __sflags (mode, &oflags)) == 0)
	return (NULL);

    if ((fp = stdioFpCreate ()) == NULL)
	return (NULL);

    if ((f = open (file, oflags, DEFFILEMODE )) < 0) 
	{ 
	fp->_flags = 0x0;			/* release */
        stdioFpDestroy (fp);			/* destroy file pointer */
	return (NULL);
	}

    fp->_file	= f;
    fp->_flags	= flags;

    /*
     * When opening in append mode, even though we use O_APPEND,
     * we need to seek to the end so that ftell() gets the right
     * answer.  If the user then alters the seek pointer, or
     * the file extends, this will fail, but there is not much
     * we can do about this.  (We could set __SAPP and check in
     * fseek and ftell.)
     */

    if (oflags & O_APPEND)
	(void) __sseek ((void *)fp, (fpos_t)0, SEEK_END);

    return (fp);
    }
/* fprintf.c - print to a file. stdio.h */

/* Copyright 1992-1995 Wind River Systems, Inc. */

/*
modification history
--------------------
01e,24jan95,rhp  doc: avoid 'L' in fprintf(), no long doubles 
                 in VxWorks (see SPR#3886)
01d,19jul94,dvs  doc tweak (SPR #2512).
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdarg.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdarg.h"
#include "fioLib.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fprintf - write a formatted string to a stream (ANSI)
*
* This routine writes output to a specified stream under control of the string
* <fmt>. The string <fmt> contains ordinary characters, which are written
* unchanged, plus conversion specifications, which cause the arguments that
* follow <fmt> to be converted and printed as part of the formatted string.
*
* The number of arguments for the format is arbitrary, but they must
* correspond to the conversion specifications in <fmt>.  If there are
* insufficient arguments, the behavior is undefined.  If the format is
* exhausted while arguments remain, the excess arguments are evaluated but
* otherwise ignored.  The routine returns when the end of the format string
* is encountered.
*
* The format is a multibyte character sequence, beginning and ending in its
* initial shift state.  The format is composed of zero or more directives:
* ordinary multibyte characters (not `%') that are copied unchanged to the
* output stream; and conversion specification, each of which results in
* fetching zero or more subsequent arguments.  Each conversion specification
* is introduced by the `%' character.  After the `%', the following appear in
* sequence:
* .iP "" 4
* Zero or more flags (in any order) that modify the meaning of the 
* conversion specification.
* .iP
* An optional minimum field width.  If the converted value has fewer
* characters than the field width, it will be padded with spaces (by
* default) on the left (or right, if the left adjustment flag, 
* described later, has been given) to the field width. The field
* width takes the form of an asterisk (`*') (described later) or a decimal
* integer.
* .iP
* An optional precision that gives the minimum number of digits to
* appear for the `d', `i', `o', `u', `x', and `X' conversions, the number of 
* digits to appear after the decimal-point character for `e', `E', and `f'
* conversions, the maximum number of significant digits for the `g' and
* `G' conversions, or the maximum number of characters to be written 
* from a string in the `s' conversion.  The precision takes the form of a 
* period (`.') followed either by an asterisk (`*') (described later) or by
* an optional decimal integer; if only the period is specified, the 
* precision is taken as zero.  If a precision appears with any other
* conversion specifier, the behavior is undefined.
* .iP
* An optional `h' specifying that a following `d', `i', `o', `u', `x', and
* `X' conversion specifier applies to a `short int' or `unsigned short int'
* argument (the argument will have been promoted according to the integral
* promotions, and its value converted to `short int' or
* `unsigned short int' before printing); an optional `h' specifying that a
* following `n' conversion specifier applies to a pointer to a `short int'
* argument; an optional `l' (el) specifying that a following `d', `i', `o',
* `u', `x', and `X' conversion specifier applies to a `long int' or
* `unsigned long int' argument; or an optional `l' specifying that a following
* `n' conversion specifier applies to a pointer to a `long int'
* argument.  If an `h' or `l' appears with any other conversion
* specifier, the behavior is undefined.
*
* \&WARNING: ANSI C also specifies an optional `L' in some of the same
* contexts as `l' above, corresponding to a `long double' argument.
* However, the current release of the VxWorks libraries does not support 
* `long double' data; using the optional `L' gives unpredictable results.
* .iP
* A character that specifies the type of conversion to be applied.
* .LP
*
* As noted above, a field width, or precision, or both, can be indicated by
* an asterisk (`*').  In this case, an `int' argument supplies the field width
* or precision.  The arguments specifying field width, or precision, or both,
* should appear (in that order) before the argument (if any) to be converted.
* A negative field width argument is taken as a `-' flag followed by a positive
* field width.  A negative precision argument is taken as if the precision
* were omitted.
*
* The flag characters and their meanings are:
* .iP `-'
* The result of the conversion will be left-justified within the field.
* (it will be right-justified if this flag is not specified.)
* .iP `+'
* The result of a signed conversion will always begin with a plus or 
* minus sign.  (It will begin with a sign only when a negative value
* is converted if this flag is not specified.)
* .iP `space'
* If the first character of a signed conversion is not a sign, or 
* if a signed conversion results in no characters, a space will be 
* prefixed to the result.  If the `space' and `+' flags both appear, the
* `space' flag will be ignored.
* .iP `#'
* The result is to be converted to an "alternate form."  For `o' conversion
* it increases the precision to force the first digit of the result to be a
* zero.  For `x' (or `X') conversion, a non-zero result will have "0x" (or
* "0X") prefixed to it.  For `e', `E', `f', `g', and `G' conversions, the
* result will always contain a decimal-point character, even if no digits
* follow it.  (Normally, a decimal-point character appears in the result of
* these conversions only if no digit follows it).  For `g' and `G'
* conversions, trailing zeros will not be removed from the result.  For
* other conversions, the behavior is undefined.
* .iP `0'
* For `d', `i', `o', `u', `x', `X', `e', `E', `f', `g', and `G' conversions,
* leading zeros (following any indication of sign or base) are used to pad
* to the field width; no space padding is performed.  If the `0' and `-'
* flags both appear, the `0' flag will be ignored.  For `d', `i', `o', `u',
* `x', and `X' conversions, if a precision is specified, the `0' flag will
* be ignored.  For other conversions, the behavior is undefined.
* .LP
*
* The conversion specifiers and their meanings are:
* .iP "`d', `i'"
* The `int' argument is converted to signed decimal in the style
* `[-]dddd'.  The precision specifies the minimum number of digits 
* to appear; if the value being converted can be represented in
* fewer digits, it will be expanded with leading zeros.  The
* default precision is 1.  The result of converting a zero value
* with a precision of zero is no characters.
* .iP "`o', `u', `x', `X'"
* The `unsigned int' argument is converted to unsigned octal (`o'),
* unsigned decimal (`u'), or unsigned hexadecimal notation (`x' or `X')
* in the style `dddd'; the letters abcdef are used for `x' conversion
* and the letters ABCDEF for `X' conversion.  The precision specifies
* the minimum number of digits to appear; if the value being 
* converted can be represented in fewer digits, it will be 
* expanded with leading zeros.  The default precision is 1.  The
* result of converting a zero value with a precision of zero is 
* no characters.
* .iP `f'
* The `double' argument is converted to decimal notation in the 
* style `[-]ddd.ddd', where the number of digits after the decimal
* point character is equal to the precision specification.  If the
* precision is missing, it is taken as 6; if the precision is zero
* and the `#' flag is not specified, no decimal-point character
* appears.  If a decimal-point character appears, at least one 
* digit appears before it.  The value is rounded to the appropriate
* number of digits.
* .iP "`e', `E'"
* The `double' argument is converted in the style `[-]d.ddde+/-dd',
* where there is one digit before the decimal-point character 
* (which is non-zero if the argument is non-zero) and the number
* of digits after it is equal to the precision; if the precision
* is missing, it is taken as 6; if the precision is zero and the 
* `#' flag is not specified, no decimal-point character appears.  The
* value is rounded to the appropriate number of digits.  The `E'
* conversion specifier will produce a number with `E' instead of `e'
* introducing the exponent.  The exponent always contains at least
* two digits.  If the value is zero, the exponent is zero.
* .iP "`g', `G'"
* The `double' argument is converted in style `f' or `e' (or in style 
* `E' in the case of a `G' conversion specifier), with the precision
* specifying the number of significant digits.  If the precision
* is zero, it is taken as 1.  The style used depends on the 
* value converted; style `e' (or `E') will be used only if the 
* exponent resulting from such a conversion is less than -4 or 
* greater than or equal to the precision.  Trailing zeros are
* removed from the fractional portion of the result; a decimal-point
* character appears only if it is followed by a digit.
* .iP `c'
* The `int' argument is converted to an `unsigned char', and the 
* resulting character is written.
* .iP `s'
* The argument should be a pointer to an array of character type.
* Characters from the array are written up to (but not including)
* a terminating null character; if the precision is specified,
* no more than that many characters are written.  If the precision
* is not specified or is greater than the size of the array, the
* array will contain a null character.
* .iP `p'
* The argument should be a pointer to `void'.  The value of the 
* pointer is converted to a sequence of printable characters,
* in hexadecimal representation (prefixed with "0x").
* .iP `n'
* The argument should be a pointer to an integer into which
* the number of characters written to the output stream
* so far by this call to fprintf() is written.  No argument is converted.
* .iP `%'
* A `%' is written.  No argument is converted.  The complete
* conversion specification is %%.
* .LP
*
* If a conversion specification is invalid, the behavior is undefined.
*
* If any argument is, or points to, a union or an aggregate (except for an 
* array of character type using `s' conversion, or a pointer using `p' 
* conversion), the behavior is undefined.
*
* In no case does a non-existent or small field width cause truncation of a
* field if the result of a conversion is wider than the field width, the
* field is expanded to contain the conversion result.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of characters written, or a negative value if an
* output error occurs.
*
* SEE ALSO: printf()
*/

int fprintf
    (
    FILE *	  fp,	/* stream to write to */
    const char *  fmt,	/* format string */
    ...			/* optional arguments to format string */
    )
    {
    int		  ret;
    va_list	  vaList;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    va_start (vaList, fmt);

    ret = vfprintf (fp, fmt, vaList);

    va_end (vaList);

    return (ret);
    }
/* fputc.c - put a character to a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fputc - write a character to a stream (ANSI)
*
* This routine writes a character <c> to a specified stream, at the
* position indicated by the stream's file position indicator
* (if defined), and advances the indicator appropriately.
*
* If the file cannot support positioning requests, or if the stream was opened 
* in append mode, the character is appended to the output stream.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The character written, or EOF if a write error occurs, with the error
* indicator set for the stream.
*
* SEE ALSO: fputs(), putc()
*/

int fputc
    (
    int		c,	/* character to write */
    FAST FILE *	fp	/* stream to write to */
    )
    {
    return (putc (c, fp));
    }
/* fputs.c - put a string of characters to a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"
#include "objLib.h"
#include "private/fvwriteP.h"


/******************************************************************************
*
* fputs - write a string to a stream (ANSI)
*
* This routine writes the string <s>, minus the terminating NULL character,
* to a specified stream. 
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* A non-negative value, or EOF if a write error occurs.
*
* SEE ALSO: fputc()
*/

int fputs
    (
    const char *  s,	/* string */
    FILE *	  fp	/* stream to write to */
    )
    {
    struct __suio uio;
    struct __siov iov;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    iov.iov_base	= (void *)s;
    iov.iov_len		= uio.uio_resid = strlen (s);
    uio.uio_iov		= &iov;
    uio.uio_iovcnt	= 1;

    return (__sfvwrite (fp, &uio));
    }

/* fread.c - read a file. stdio.h */

/* Copyright 1992-2002 Wind River Systems, Inc. */

/*
modification history
--------------------
01f,21jan02,jkf  SPR#72774, fread is checking for NULL on wrong arg
01e,12dec01,jkf  fixing SPR#72128, fread should check for NULL before bcopy
01d,10nov01,jkf  SPR#70967, fread returns wrong value when 3rd arg == 0
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	    smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fread - read data into an array (ANSI)
*
* This routine reads, into the array <buf>, up to <count> elements of size
* <size>, from a specified stream <fp>.  The file position indicator for the
* stream (if defined) is advanced by the number of characters successfully
* read.  If an error occurs, the resulting value of the file position
* indicator for the stream is indeterminate.  If a partial element is read,
* its value is indeterminate.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of elements successfully read, which may be less than <count>
* if a read error or end-of-file is encountered; or zero if <size> or
* <count> is zero, with the contents of the array and the state of the
* stream remaining unchanged.
*/

int fread
    (
    void *	buf,	/* where to copy data */
    size_t	size, 	/* element size */
    size_t	count,	/* no. of elements */
    FAST FILE *	fp	/* stream to read from */
    )
    {
    FAST size_t resid;
    FAST char *	p;
    FAST int	r;
    size_t	total;

    /* SPR#72128, check for NULL args before bcopy */

    if ((NULL == fp) || (NULL == buf))
	{
        return (0);
	}

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (0);

    /* 
     * SPR#70967: fread returns wrong value when 3rd arg == 0 
     *
     * ANSI specification declares fread() returns zero if size 
     * _or_ count is zero.  But the vxWorks implementation returned 
     * non-zero given the following input case:
     *
     * fread ( buf , 0 ,1 ,fp );
     *
     * Old (broken) code:
     *
     * if ((resid = (count * size)) == 0)
     *     return (count);     
     *
     * New (fixed) code:
     *
     * if (0 == (resid = (count * size)))
     *     return (0);     
     */

    if (0 == (resid = (count * size)))
	return (0);

    if (fp->_r < 0)
	fp->_r = 0;

    total = resid;
    p	  = buf;

    while (resid > (r = fp->_r)) 
	{
	(void) bcopy ((void *)fp->_p, (void *)p, (size_t)r);

	fp->_p	+= r;
	p	+= r;
	resid	-= r;

	if (__srefill (fp))				/* fp->_r = 0 is done */
		return ((total - resid) / size);	/* partial result */
	}

    (void) bcopy ((void *)fp->_p, (void *)p, resid);

    fp->_r	-= resid;
    fp->_p	+= resid;

    return (count);
    }

/* freopen.c - reopen a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  added memory reclaimation.
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, sys/types.h, sys/stat.h, fcntl.h, error.h, unistd.h, 
	      stdlib.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "ioLib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "errno.h"
#include "unistd.h"
#include "stdlib.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* freopen - open a file specified by name (ANSI)
*
* This routine opens a file whose name is the string pointed to by <file>
* and associates it with a specified stream <fp>.
* The <mode> argument is used just as in the fopen() function.
*
* This routine first attempts to close any file that is associated
* with the specified stream.  Failure to close the file successfully is
* ignored. The error and end-of-file indicators for the stream are cleared.
*
* Typically, freopen() is used to attach the already-open streams
* `stdin', `stdout', and `stderr' to other files.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The value of <fp>, or a null pointer if the open operation fails.
*
* SEE ALSO: fopen()
*/

FILE * freopen
    (
    const char *  file,		/* name of file */
    const char *  mode,		/* mode */
    FAST FILE *	  fp		/* stream */
    )
    {
    FAST int	f;
    int		flags;
    int		oflags;
    int		sverrno;
    BOOL	isopen = TRUE;			/* its already open */

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (NULL);

    if ((flags = __sflags (mode, &oflags)) == 0) 
	{
	(void) fclose (fp);
	return (NULL);
	}

    /*
     * There are actually programs that depend on being able to "freopen"
     * descriptors that weren't originally open.  Keep this from breaking.
     * Remember whether the stream was open to begin with, and which file
     * descriptor (if any) was associated with it.  If it was attached to
     * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
     * should work.  This is unnecessary if it was not a Unix file.
     */

    if (fp->_flags & __SWR)			/* flush the stream though */
	(void) __sflush(fp); 			/* ANSI doesn't require this */

    if (fp->_file < 0)				/* need to close the fp? */
	{
	(void) __sclose (fp);
	isopen = FALSE;
	}

    /* Get a new descriptor to refer to the new file. */

    f = open (file, oflags, DEFFILEMODE);

    if ((f < 0) && isopen)
	{
	/* If out of fd's close the old one and try again. */

	if ((errno == ENFILE) || (errno == EMFILE))
	    {
	    (void) __sclose (fp);
	    isopen = FALSE;
	    f = open (file, oflags, DEFFILEMODE);
	    }
	}

    sverrno = errno;

    /*
     * Finish closing fp.  Even if the open succeeded above, we cannot
     * keep fp->_base: it may be the wrong size.  This loses the effect
     * of any setbuffer calls, but stdio has always done this before.
     */

    if (isopen)
	(void) __sclose (fp);

    if (fp->_flags & __SMBF)
	free ((char *)fp->_bf._base);

    fp->_w		= 0;
    fp->_r		= 0;
    fp->_p		= NULL;
    fp->_bf._base	= NULL;
    fp->_bf._size	= 0;
    fp->_lbfsize	= 0;

    if (HASUB(fp))
	FREEUB(fp);

    fp->_ub._size	= 0;

    if (HASLB(fp))
	FREELB(fp);

    fp->_lb._size	= 0;

    if ((f < 0) && (fp->_flags != 0))
	{				/* did not get it after all */
	fp->_flags = 0;			/* set it free */
	stdioFpDestroy (fp);		/* destroy file pointer */
	errno = sverrno;		/* restore in case _close clobbered */
	return (NULL);
	}

    fp->_flags	= flags;
    fp->_file	= f;

    return (fp);
    }
/* fscanf.c - scan a file. stdio.h */

/* Copyright 1992-1995 Wind River Systems, Inc. */

/*
modification history
--------------------
01d,24jan95,rhp  doc: avoid 'L' in fscanf(), no long doubles 
                 in VxWorks (see SPR#3886)
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdarg.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "fioLib.h"
#include "stdarg.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* fscanf - read and convert characters from a stream (ANSI)
* 
* This routine reads characters from a specified stream, and interprets them
* according to format specifications in the string <fmt>, which specifies
* the admissible input sequences and how they are to be converted for
* assignment, using subsequent arguments as pointers to the objects to
* receive the converted input.
*
* If there are insufficient arguments for the format, the behavior is
* undefined.  If the format is exhausted while arguments remain, the excess
* arguments are evaluated but are otherwise ignored.
*
* The format is a multibyte character sequence, beginning and ending in
* its initial shift state.  The format is composed of zero or more directives:
* one or more white-space characters; an ordinary multibyte character (neither
* `%' nor a white-space character); or a conversion specification.  Each
* conversion specification is introduced by the `%' character.  After the `%',
* the following appear in sequence:
*
* .iP "" 4
* An optional assignment-suppressing character `*'.
* .iP
* An optional non-zero decimal integer that specifies the maximum field 
* width.
* .iP
* An optional `h' or `l' (el) indicating the size of the receiving 
* object.  The conversion specifiers `d', `i', and `n' should be preceded by
* `h' if the corresponding argument is a pointer to `short int' rather
* than a pointer to `int', or by `l' if it is a pointer to `long int'.
* Similarly, the conversion specifiers `o', `u', and `x' shall be preceded
* by `h' if the corresponding argument is a pointer to `unsigned short int'
* rather than a pointer to `unsigned int', or by `l' if it is a pointer to
* `unsigned long int'.  Finally, the conversion specifiers `e', `f', and `g' 
* shall be preceded by `l' if the corresponding argument is a pointer to
* `double' rather than a pointer to `float'.  If an `h' or `l' appears
* with any other conversion specifier, the behavior is undefined.
*
* \&WARNING: ANSI C also specifies an optional `L' in some of the same
* contexts as `l' above, corresponding to a `long double *' argument.
* However, the current release of the VxWorks libraries does not support 
* `long double' data; using the optional `L' gives unpredictable results.
* .iP
* A character that specifies the type of conversion to be applied.  The
* valid conversion specifiers are described below.
* .LP
*
* The fscanf() routine executes each directive of the format in turn.  If a 
* directive fails, as detailed below, fscanf() returns.  Failures
* are described as input failures (due to the unavailability of input
* characters), or matching failures (due to inappropriate input).
* 
* A directive composed of white-space character(s) is executed by reading
* input up to the first non-white-space character (which remains unread),
* or until no more characters can be read.
*
* A directive that is an ordinary multibyte character is executed by reading
* the next characters of the stream.  If one of the characters differs from
* one comprising the directive, the directive fails, and the differing and
* subsequent characters remain unread.
*
* A directive that is a conversion specification defines a set of matching
* input sequences, as described below for each specifier.  A conversion
* specification is executed in the following steps:
*
* Input white-space characters (as specified by the isspace() function) are 
* skipped, unless the specification includes a `[', `c', or `n' specifier.
*
* An input item is read from the stream, unless the specification includes
* an `n' specifier.  An input item is defined as the longest matching
* sequence of input characters, unless that exceeds a specified field width,
* in which case it is the initial subsequence of that length in the
* sequence.  The first character, if any, after the input item remains
* unread.  If the length of the input item is zero, the execution of the
* directive fails:  this condition is a matching failure, unless an error
* prevented input from the stream, in which case it is an input failure.
*
* Except in the case of a `%' specifier, the input item is converted to a
* type appropriate to the conversion specifier.  If the input item is not a
* matching sequence, the execution of the directive fails:  this condition
* is a matching failure.  Unless assignment suppression was indicated by a
* `*', the result of the conversion is placed in the object pointed to by
* the first argument following the <fmt> argument that has not already
* received a conversion result.  If this object does not have an appropriate
* type, or if the result of the conversion cannot be represented in the
* space provided, the behavior is undefined.
*
* The following conversion specifiers are valid:
*
* .iP `d'
* Matches an optionally signed decimal integer whose format is
* the same as expected for the subject sequence of the strtol()
* function with the value 10 for the <base> argument.  The 
* corresponding argument should be a pointer to `int'.
* .iP `i'
* Matches an optionally signed integer, whose format is the
* same as expected for the subject sequence of the strtol()
* function with the value 0 for the <base> argument.  The 
* corresponding argument should be a pointer to `int'.
* .iP `o'
* Matches an optionally signed octal integer, whose format is the
* same as expected for the subject sequence of the strtoul()
* function with the value 8 for the <base> argument.  The
* corresponding argument should be a pointer to `unsigned int'.
* .iP `u'
* Matches an optionally signed decimal integer, whose format is 
* the same as expected for the subject sequence of the strtoul()
* function with the value 10 for the <base> argument.  The
* corresponding argument should be a pointer to `unsigned int'.
* .iP `x'
* Matches an optionally signed hexadecimal integer, whose format is
* the same as expected for the subject sequence of the strtoul()
* function with the value 16 for the <base> argument.  The
* corresponding argument should be a pointer to `unsigned int'.
* .iP "`e', `f', `g'"
* Match an optionally signed floating-point number, whose format
* is the same as expected for the subject string of the strtod()
* function.  The corresponding argument should be a pointer to `float'.
* .iP `s'
* Matches a sequence of non-white-space characters.  The 
* corresponding argument should be a pointer to the initial
* character of an array large enough to accept the sequence
* and a terminating null character, which will be added 
* automatically.
* .iP `['
* Matches a non-empty sequence of characters from a set of 
* expected characters (the `scanset').  The corresponding argument
* should be a pointer to the initial character of an array large
* enough to accept the sequence and a terminating null character,
* which is added automatically.  The conversion specifier
* includes all subsequent character in the format string, up to
* and including the matching right bracket (`]').  The characters
* between the brackets (the `scanlist') comprise the scanset,
* unless the character after the left bracket is a circumflex (`^')
* in which case the scanset contains all characters that do not
* appear in the scanlist between the circumflex and the right
* bracket.  If the conversion specifier begins with "[]" or "[^]", the
* right bracket character is in the scanlist and the next 
* right bracket character is the matching right bracket that ends
* the specification; otherwise the first right bracket character
* is the one that ends the specification.
* .iP `c'
* Matches a sequence of characters of the number specified by the
* field width (1 if no field width is present in the directive).
* The corresponding argument should be a pointer to the initial 
* character of an array large enough to accept the sequence.
* No null character is added.
* .iP `p'
* Matches an implementation-defined set of sequences, which should be
* the same as the set of sequences that may be produced by the %p
* conversion of the fprintf() function.  The corresponding argument
* should be a pointer to a pointer to `void'.  VxWorks defines its
* pointer input field to be consistent with pointers written by the
* fprintf() function ("0x" hexadecimal notation).  If the input item is
* a value converted earlier during the same program execution, the
* pointer that results should compare equal to that value; otherwise
* the behavior of the %p conversion is undefined.
* .iP `n'
* No input is consumed.  The corresponding argument should be a pointer to
* `int' into which the number of characters read from the input stream so
* far by this call to fscanf() is written.  Execution of a %n directive does
* not increment the assignment count returned when fscanf() completes
* execution.
* .iP `%'
* Matches a single `%'; no conversion or assignment occurs.  The
* complete conversion specification is %%.
* .LP
*
* If a conversion specification is invalid, the behavior is undefined.
*
* The conversion specifiers `E', `G', and `X' are also valid and behave the
* same as `e', `g', and `x', respectively.
*
* If end-of-file is encountered during input, conversion is terminated.  If 
* end-of-file occurs before any characters matching the current directive
* have been read (other than leading white space, where permitted), execution
* of the current directive terminates with an input failure; otherwise, unless
* execution of the current directive is terminated with a matching failure,
* execution of the following directive (if any) is terminated with an input
* failure.
*
* If conversion terminates on a conflicting input character, the offending
* input character is left unread in the input stream.  Trailing white space
* (including new-line characters) is left unread unless matched by a
* directive.  The success of literal matches and suppressed assignments is
* not directly determinable other than via the %n directive.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of input items assigned, which can be fewer than provided for,
* or even zero, in the event of an early matching failure; or EOF if an
* input failure occurs before any conversion.
*
* SEE ALSO: scanf(), sscanf()
*/

int fscanf
    (
    FILE *	  fp,	/* stream to read from */
    char const *  fmt,	/* format string */
    ...			/* arguments to format string */
    ) 
    {
    int     nArgs;
    int     unget;
    va_list vaList;	/* vararg list */

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    va_start (vaList, fmt);
    nArgs = fioScanV (fmt, fgetc, (int) fp, &unget, vaList);
    va_end (vaList);

    if (unget != -1)
	ungetc (unget, fp);

    return (nArgs);
    }
/* fseek.c - seek a position in a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio, removed fstat().
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, sys/types.h, sys/stat.h, fcntl.h, stdlib.h, errno.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "ioLib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "errno.h"
#include "objLib.h"
#include "private/stdioP.h"

#define	POS_ERR	(-(fpos_t)1)


/******************************************************************************
*
* fseek - set the file position indicator for a stream (ANSI)
*
* This routine sets the file position indicator for a specified stream.
* For a binary stream, the new position, measured in characters from the 
* beginning of the file, is obtained by adding <offset> to the position 
* specified by <whence>, whose possible values are:
* .iP SEEK_SET 16
* the beginning of the file.
* .iP SEEK_CUR
* the current value of the file position indicator.
* .iP SEEK_END
* the end of the file.
* .LP
* A binary stream does not meaningfully
* support fseek() calls with a <whence> value of SEEK_END.
*
* For a text stream, either <offset> is zero, or <offset> is a value
* returned by an earlier call to ftell() on the stream, in which case
* <whence> should be SEEK_SET.
*
* A successful call to fseek() clears the end-of-file indicator for the
* stream and undoes any effects of ungetc() on the same stream.  After an
* fseek() call, the next operation on an update stream can be either input
* or output.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: Non-zero only for a request that cannot be satisfied.
*
* ERRNO: EINVAL
*
* SEE ALSO: ftell()
*/

int fseek
    (
    FAST FILE *	fp,		/* stream */
    long	offset,		/* offset from <whence> */
    int		whence		/* position to offset from: */
				/* SEEK_SET = beginning */
				/* SEEK_CUR = current position */
				/* SEEK_END = end-of-file */
    )
    {
    fpos_t	target;
    fpos_t	curoff;
    size_t	n;
    struct stat st;
    int		havepos;
    BOOL	doStat;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    /*
     * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
     * After this, whence is either SEEK_SET or SEEK_END.
     */

    switch (whence) 
	{
	case SEEK_CUR:
		/*
		 * In order to seek relative to the current stream offset,
		 * we have to first find the current stream offset a la
		 * ftell (see ftell for details).
		 */
		if (fp->_flags & __SOFF)
		    curoff = fp->_offset;
		else 
		    {
		    curoff = __sseek (fp, (fpos_t)0, SEEK_CUR);
		    if (curoff == -1L)
			return (EOF);
		    }

		if (fp->_flags & __SRD) 
		    {
		    curoff -= fp->_r;
		    if (HASUB(fp))
			curoff -= fp->_ur;
		    }
		else if (fp->_flags & __SWR && fp->_p != NULL)
		    curoff += fp->_p - fp->_bf._base;

		offset += curoff;
		whence	= SEEK_SET;
		havepos = 1;
		break;

	case SEEK_SET:
	case SEEK_END:
		curoff = 0;		/* XXX just to keep gcc quiet */
		havepos = 0;
		break;

	default:
		errno = EINVAL;
		return (EOF);
	}

    /*
     * Can only optimise if:
     *	reading (and not reading-and-writing);
     *	not unbuffered; and
     *	this is a `regular' Unix file (and hence seekfn==__sseek).
     * We must check __NBF first, because it is possible to have __NBF
     * and __SOPT both set.
     */

    if (fp->_bf._base == NULL)
	__smakebuf (fp);

    if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
	goto dumb;


    doStat = ioctl (fp->_file, FIOFSTATGET, (int)&st);

    if ((fp->_flags & __SOPT) == 0) 
	{
	if ((fp->_file < 0 || (doStat) ||
	    (st.st_mode & S_IFMT) != S_IFREG)) 
	    {
	    fp->_flags |= __SNPT;
	    goto dumb;
	    }

	fp->_blksize 	 = st.st_blksize;
	fp->_flags	|= __SOPT;
	}

    /*
     * We are reading; we can try to optimise.
     * Figure out where we are going and where we are now.
     */

    if (whence == SEEK_SET)
	target = offset;
    else 
	{
	if (doStat)
	    goto dumb;

	target = st.st_size + offset;
	}

    if (!havepos) 
	{
	if (fp->_flags & __SOFF)
	    curoff = fp->_offset;
	else 
	    {
	    curoff = __sseek (fp, 0L, SEEK_CUR);

	    if (curoff == POS_ERR)
		goto dumb;
	    }

	curoff -= fp->_r;

	if (HASUB(fp))
	    curoff -= fp->_ur;
	}

    /*
     * Compute the number of bytes in the input buffer (pretending
     * that any ungetc() input has been discarded).  Adjust current
     * offset backwards by this count so that it represents the
     * file offset for the first byte in the current input buffer.
     */

    if (HASUB(fp)) 
	{
	n	= fp->_up - fp->_bf._base;
	curoff -= n;
	n      += fp->_ur;
	} 
    else 
	{
	n	= fp->_p - fp->_bf._base;
	curoff -= n;
	n      += fp->_r;
	}

    /*
     * If the target offset is within the current buffer,
     * simply adjust the pointers, clear EOF, undo ungetc(),
     * and return.  (If the buffer was modified, we have to
     * skip this; see fgetline.c.)
     */

    if (((fp->_flags & __SMOD) == 0) && 
	(target >= curoff) && 
	(target < (curoff + n))) 
	{
	FAST int o = target - curoff;

	fp->_p = fp->_bf._base + o;
	fp->_r = n - o;

	if (HASUB(fp))
	    FREEUB(fp);

	fp->_flags &= ~__SEOF;

	return (0);
	}

    /*
     * The place we want to get to is not within the current buffer,
     * but we can still be kind to the kernel copyout mechanism.
     * By aligning the file offset to a block boundary, we can let
     * the kernel use the VM hardware to map pages instead of
     * copying bytes laboriously.  Using a block boundary also
     * ensures that we only read one block, rather than two.
     */

    curoff = target & ~(fp->_blksize - 1);

    if (__sseek (fp, curoff, SEEK_SET) == POS_ERR)
	goto dumb;

    fp->_r = 0;

    if (HASUB(fp))
	FREEUB(fp);

    fp->_flags &= ~__SEOF;
    n = target - curoff;

    if (n) 
	{
	if (__srefill (fp) || fp->_r < n)
	    goto dumb;
	fp->_p += n;
	fp->_r -= n;
	}

    return (0);

    /*
     * We get here if we cannot optimise the seek ... just
     * do it.  Allow the seek function to change fp->_bf._base.
     */
dumb:
    if ((__sflush (fp)) || (__sseek (fp, offset, whence) == POS_ERR)) 
	return (EOF);

    /* success: clear EOF indicator and discard ungetc() data */

    if (HASUB(fp))
	FREEUB(fp);

    fp->_p = fp->_bf._base;
    fp->_r = 0;

    /* fp->_w = 0; */	/* unnecessary (I think...) */

    fp->_flags &= ~__SEOF;

    return (0);
    }
/* fsetpos.c - set a position in a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"


/******************************************************************************
*
* fsetpos - set the file position indicator for a stream (ANSI)
*
* This routine sets the file position indicator for a specified stream <iop>
* according to the value of the object pointed to by <pos>, which is a value
* obtained from an earlier call to fgetpos() on the same stream.
*
* A successful call to fsetpos() clears the end-of-file indicator for the
* stream and undoes any effects of ungetc() on the same stream.  After an
* fsetpos() call, the next operation on an update stream may be either input
* or output.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* Zero, or non-zero if the call fails, with `errno' set to indicate the error.
*
* SEE ALSO: fgetpos()
*/

int fsetpos 
    (
    FILE *	    iop,	/* stream */
    const fpos_t *  pos		/* position, obtained by fgetpos() */
    )
    {
    return (fseek (iop, (long)*pos, SEEK_SET));
    }
/* ftell.c - remember a position in a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, error.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "errno.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* ftell - return the current value of the file position indicator for a stream (ANSI)
*
* This routine returns the current value of the file position indicator for
* a specified stream.  For a binary stream, the value is the number of
* characters from the beginning of the file.  For a text stream, the file
* position indicator contains unspecified information, usable by fseek() for
* returning the file position indicator to its position at the time of the
* ftell() call; the difference between two such return values is not
* necessary a meaningful measure of the number of characters written or read.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The current value of the file position indicator,
* or -1L if unsuccessful, with `errno' set to indicate the error.
*
* SEE ALSO: fseek()
*/

long ftell
    (
    FAST FILE *  fp	/* stream */
    )
    {
    FAST fpos_t pos;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    /*
     * Find offset of underlying I/O object, then
     * adjust for buffered bytes.
     */

    if (fp->_flags & __SOFF)
	pos = fp->_offset;
    else 
	{
	pos = __sseek (fp, (fpos_t)0, SEEK_CUR);

	if (pos == -1L)
	    return (pos);
	}

    if (fp->_flags & __SRD) 
	{
	/*
	 * Reading.  Any unread characters (including
	 * those from ungetc) cause the position to be
	 * smaller than that in the underlying object.
	 */

	pos -= fp->_r;

	if (HASUB(fp))
	    pos -= fp->_ur;
	} 
    else if (fp->_flags & __SWR && fp->_p != NULL) 
	{
	/*
	 * Writing.  Any buffered characters cause the
	 * position to be greater than that in the
	 * underlying object.
	 */

	pos += fp->_p - fp->_bf._base;
	}

    return (pos);
    }
/* fwrite.c - write to a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "objLib.h"
#include "private/stdioP.h"
#include "private/fvwriteP.h"


/******************************************************************************
*
* fwrite - write from a specified array (ANSI)
*
* This routine writes, from the array <buf>, up to <count> elements whose
* size is <size>, to a specified stream.  The file position indicator for
* the stream (if defined) is advanced by the number of characters
* successfully written.  If an error occurs, the resulting value of the file
* position indicator for the stream is indeterminate.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of elements successfully written, which will be less than
* <count> only if a write error is encountered.
*/

int fwrite
    (
    const void *  buf,		/* where to copy from */
    size_t	  size,		/* element size */
    size_t	  count,	/* no. of elements */
    FILE *	  fp		/* stream to write to */
    )
    {
    size_t		n;
    struct __suio	uio;
    struct __siov	iov;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (0);

    iov.iov_base	= (void *)buf;
    uio.uio_resid	= iov.iov_len = n = (count * size);
    uio.uio_iov		= &iov;
    uio.uio_iovcnt	= 1;

    /* The usual case is success (__sfvwrite returns 0);
     * skip the divide if this happens, since divides are
     * generally slow and since this occurs whenever size==0.
     */

    if (__sfvwrite(fp, &uio) == 0)
	return (count);

    return ((n - uio.uio_resid) / size);
    }
/* getc.c - get a character from a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "objLib.h"
#include "private/stdioP.h"

#undef getc

/******************************************************************************
*
* getc - return the next character from a stream (ANSI)
*
* This routine is equivalent to fgetc(), except that if it is implemented as
* a macro, it may evaluate <fp> more than once; thus the argument should
* never be an expression with side effects.
*
* If the stream is at end-of-file, the end-of-file indicator for the stream is
* set; if a read error occurs, the error indicator is set.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The next character from the stream, or EOF if the stream is at end-of-file
* or a read error occurs.
*
* SEE ALSO: fgetc()
*/

int getc
    (
    FILE *  fp	/* input stream */
    )
    {
    int ch;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    if ((ch = __sgetc(fp)) == EOF)
	fp->_flags |= __SEOF;

    return (ch);
    }
/* getchar.c - get a character from a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "private/stdioP.h"

#undef getchar

/******************************************************************************
*
* getchar - return the next character from the standard input stream (ANSI)
*
* This routine returns the next character from the standard input stream 
* and advances the file position indicator.
*
* It is equivalent to getc() with the stream argument `stdin'.
*
* If the stream is at end-of-file, the end-of-file indicator is
* set; if a read error occurs, the error indicator is set.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The next character from the standard input stream, or EOF if the stream is
* at end-of-file or a read error occurs.
*
* SEE ALSO: getc(), fgetc()
*/

int getchar (void)
    {
    return (getc (stdin));
    }
/* gets.c - get a string of characters from a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, unistd.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "unistd.h"
#include "private/stdioP.h"


/******************************************************************************
*
* gets - read characters from the standard input stream (ANSI)
*
* This routine reads characters from the standard input stream into the
* array <buf> until end-of-file is encountered or a new-line is read.
* Any new-line character is discarded, and a null character is written 
* immediately after the last character read into the array.
*
* If end-of-file is encountered and no characters have been read,
* the contents of the array remain unchanged.  If a read error
* occurs, the array contents are indeterminate.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* A pointer to <buf>, or a null pointer if (1) end-of-file is encountered
* and no characters have been read, or (2) there is a read error.
*/

char * gets 
    (
    char *	buf	/* output array */
    )
    {
    FAST int	c;
    FAST char *	s;

    for (s = buf; (c = getchar()) != '\n';)
	{
	if (c == EOF)
	    {
	    if (s == buf)
		return (NULL);
	    else
		break;
	    }
	else
	    *s++ = c;
	}

    *s = EOS;

    return (buf);
    }
/* getw.c     - get a word. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  written.
*/

/*
DESCRIPTION

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"

/******************************************************************************
*
* getw - read the next word (32-bit integer) from a stream
*
* This routine reads the next 32-bit quantity from a specified stream.
* It returns EOF on end-of-file or an error; however, this is also a
* valid integer, thus feof() and ferror() must be used to check for
* a true end-of-file.
*
* This routine is provided for compatibility with earlier VxWorks releases.
*
* INCLUDE FILES: stdio.h
*
* RETURN: A 32-bit number from the stream, or EOF on either end-of-file
* or an error.
*
* SEE ALSO: putw()
*/

int getw
    (
    FILE *	fp	/* stream to read from */
    )
    {
    int x;

    return (fread ((void *)&x, sizeof (x), 1, fp) == 1 ? x : EOF);
    }
/* perror.c - print error value. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/* 
modification history 
-------------------- 
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  written.
*/ 

/*
DESCRIPTION

INCLUDE FILE: stdio.h, error.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"
#include "errno.h"


/******************************************************************************
*
* perror - map an error number in `errno' to an error message (ANSI)
*
* This routine maps the error number in the integer expression `errno' to an
* error message.  It writes a sequence of characters to the standard error
* stream as follows:  first (if <__s> is not a null pointer and the character
* pointed to by <__s> is not the null character), the string pointed to by
* <__s> followed by a colon (:) and a space; then an appropriate error
* message string followed by a new-line character.  The contents of the
* error message strings are the same as those returned by strerror() with
* the argument `errno'.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: N/A
*
* SEE ALSO: strerror()
*/

void perror 
    (
    const char *  __s	/* error string */
    )
    {
    if ((__s) && (*__s != EOS))
    	fprintf (stderr, "%s: ", __s);

    fprintf (stderr, "%s\n", strerror (errno));
    }
/* putc.c - put a character. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "private/stdioP.h"

#undef putc

/******************************************************************************
*
* putc - write a character to a stream (ANSI)
*
* This routine writes a character <c> to a specified stream, at the
* position indicated by the stream's file position indicator
* (if defined), and advances the indicator appropriately.
*
* This routine is equivalent to fputc(), except that if it is implemented as
* a macro, it may evaluate <fp> more than once; thus, the argument should
* never be an expression with side effects.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The character written, or EOF if a write error occurs, with the error
* indicator set for the stream.
*
* SEE ALSO: fputc()
*/

int putc
    (
    int		 c,	/* character to write */
    FAST FILE *	 fp	/* stream to write to */
    )
    {
    return (__sputc(c, fp));
    }
/* putchar.c - put a character to standard output. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "private/stdioP.h"

#undef putchar

/******************************************************************************
*
* putchar - write a character to the standard output stream (ANSI)
* 
* This routine writes a character <c> to the standard output stream, at the
* position indicated by the stream's file position indicator
* (if defined), and advances the indicator appropriately.
*
* This routine is equivalent to putc() with a second argument of `stdout'.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The character written, or EOF if a write error occurs, with the error
* indicator set for the standard output stream.
*
* SEE ALSO: putc(), fputc()
*/

int putchar
    (
    int c	/* character to write */
    )
    {
    return (__sputc(c, stdout));
    }
/* puts.c - put a string of characters. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"
#include "private/fvwriteP.h"


/******************************************************************************
*
* puts - write a string to the standard output stream (ANSI)
*
* This routine writes to the standard output stream a specified string <s>,
* minus the terminating null character, and appends a new-line character to
* the output.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* A non-negative value, or EOF if a write error occurs.
*
* SEE ALSO: fputs()
*/

int puts
    (
    char const *  s	/* string to write */
    )
    {
    size_t c = strlen (s);
    struct __suio uio;
    struct __siov iov [2];

    iov[0].iov_base	= (void *)s;
    iov[0].iov_len	= c;
    iov[1].iov_base	= "\n";
    iov[1].iov_len	= 1;

    uio.uio_resid	= c + 1;
    uio.uio_iov		= &iov [0];
    uio.uio_iovcnt	= 2;

    return ((__sfvwrite (stdout, &uio)) ? (EOF) : ('\n'));
    }
/* putw.c - put a word. stdio.h */ 
 
/* Copyright 1992-1993 Wind River Systems, Inc. */
 
/* 
modification history 
-------------------- 
01d,05mar93,jdi  documentation cleanup for 5.1.
01c,21sep92,smb  tweaks for mg
01b,20sep92,smb  documentation additions
01a,29jul92,smb  written.
*/ 
 
/* 
DESCRIPTION 
 
INCLUDE FILE: stdio.h, string.h 
 
SEE ALSO: American National Standard X3.159-1989 

NOMANUAL
*/ 
 
#include "vxWorks.h" 
#include "stdio.h" 
 
/******************************************************************************
*
* putw - write a word (32-bit integer) to a stream
*
* This routine appends the 32-bit quantity <w> to a specified stream.
*
* This routine is provided for compatibility with earlier VxWorks releases.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: The value written.
*/

int putw
    (
    int     w, 	/* word (32-bit integer) */
    FILE *  fp	/* output stream */
    )
    {
    return (fwrite ((void *) &w, sizeof (int), 1, fp) == 1 ? w : EOF);
    }
/* rewind.c - rewind a stream. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, error.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "objLib.h"
#include "errno.h"


/******************************************************************************
*
* rewind - set the file position indicator to the beginning of a file (ANSI)
*
* This routine sets the file position indicator for a specified stream to
* the beginning of the file.
* 
* It is equivalent to:
* .CS
*     (void) fseek (fp, 0L, SEEK_SET);
* .CE
* except that the error indicator for the stream is cleared.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: N/A
*
* SEE ALSO: fseek(), ftell()
*/

void rewind
    (
    FILE *  fp	/* stream */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return;

    (void) fseek (fp, 0L, SEEK_SET);
    clearerr(fp);
    }
/* scanf.c - scan a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  rewritten to usr fioScanV()
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdarg.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdarg.h"
#include "fioLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* scanf - read and convert characters from the standard input stream (ANSI)
*
* This routine reads input from the standard input stream under the control
* of the string <fmt>.  It is equivalent to fscanf() with an <fp> argument
* of `stdin'.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of input items assigned, which can be fewer than provided for,
* or even zero, in the event of an early matching failure; or EOF if an
* input failure occurs before any conversion.
*
* SEE ALSO: fscanf(), sscanf()
*/

int scanf
    (
    char const *  fmt,	/* format string */
    ...			/* arguments to format string */
    )
    {
    int     nArgs;
    int     unget;
    va_list vaList;	/* vararg list */

    va_start (vaList, fmt);
    nArgs = fioScanV (fmt, fgetc, (int) stdin, &unget, vaList);
    va_end (vaList);

    if (unget != -1)
	ungetc (unget, stdin);

    return (nArgs);
    }
/* setbuf.c - set buffered mode. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* setbuf - specify the buffering for a stream (ANSI)
*
* Except that it returns no value, this routine is equivalent to setvbuf()
* invoked with the <mode> _IOFBF (full buffering) and <size> BUFSIZ, or (if
* <buf> is a null pointer), with the <mode> _IONBF (no buffering).
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: N/A
*
* SEE ALSO: setvbuf()
*/

void setbuf
    (
    FILE *  fp,	 /* stream to set buffering for */
    char *  buf	 /* buffer to use */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return;

    (void) setvbuf (fp, buf, ((buf) ? (_IOFBF) : (_IONBF)), BUFSIZ);
    }
/* setbuffer.c- file for stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* setbuffer - specify buffering for a stream
*
* This routine specifies a buffer <buf> to be used for a stream in place of the
* automatically allocated buffer.  If <buf> is NULL, the stream is unbuffered.
* This routine should be called only after the stream has been associated with
* an open file and before any other operation is performed on the stream.
*
* This routine is provided for compatibility with earlier VxWorks releases.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: N/A
*
* SEE ALSO: setvbuf()
*/

void setbuffer
    (
    FAST FILE *	 fp,	/* stream to set buffering for */
    char *	 buf,	/* buffer to use */
    int		 size	/* buffer size */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return;

    (void) setvbuf (fp, buf, ((buf) ? (_IOFBF) : (_IONBF)), size);
    }

/******************************************************************************
*
* setlinebuf - set line buffering for standard output or standard error
*
* This routine changes `stdout' or `stderr' streams from block-buffered or
* unbuffered to line-buffered.  Unlike setbuf(), setbuffer(), or setvbuf(), it
* can be used at any time the stream is active.
* 
* A stream can be changed from unbuffered or line-buffered to fully buffered
* using freopen().  A stream can be changed from fully buffered or
* line-buffered to unbuffered using freopen() followed by setbuf() with a
* buffer argument of NULL.
* 
* This routine is provided for compatibility with earlier VxWorks releases.
* 
* INCLUDE: stdio.h 
*
* RETURNS: OK, or ERROR if <fp> is not a valid stream.
*/

int setlinebuf
    (
    FILE *  fp	/* stream - stdout or stderr */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    (void) setvbuf (fp, (char *)NULL, _IOLBF, (size_t)0);
    return (OK);						/* ??? */
    }
/* setvbuf.c - set buffered mode. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdlib.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* setvbuf - specify buffering for a stream (ANSI)
*
* This routine sets the buffer size and buffering mode for a specified
* stream.  It should be called only after the stream has been associated
* with an open file and before any other operation is performed on the
* stream.  The argument <mode> determines how the stream will be buffered,
* as follows:
* .iP _IOFBF 12
* input/output is to be fully buffered.
* .iP _IOLBF
* input/output is to be line buffered.
* .iP _IONBF
* input/output is to be unbuffered. 
* .LP
*
* If <buf> is not a null pointer, the array it points to may be used instead
* of a buffer allocated by setvbuf().  The argument <size> specifies
* the size of the array.  The contents of the array at any time are 
* indeterminate.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* Zero, or non-zero if <mode> is invalid or the request cannot be honored.
*/

int setvbuf
    (
    FAST FILE *	 fp,	/* stream to set buffering for */
    char *	 buf,	/* buffer to use (optional) */
    FAST int	 mode,	/* _IOFBF = fully buffered */
			/* _IOLBF = line buffered */
			/* _IONBF = unbuffered */
    FAST size_t  size	/* buffer size */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);

    /*
     * Verify arguments.  The `int' limit on `size' is due to this
     * particular implementation.
     */

    if (((mode != _IOFBF) && (mode != _IOLBF) && (mode != _IONBF)) ||
	((int)size < 0))
	return (EOF);

    /*
     * Write current buffer, if any; drop read count, if any.
     * Make sure putc() will not think fp is line buffered.
     * Free old buffer if it was from malloc().  Clear line and
     * non buffer flags, and clear malloc flag.
     */

    (void) __sflush (fp);

    fp->_r	 = 0;
    fp->_lbfsize = 0;

    if (fp->_flags & __SMBF)
	free ((void *)fp->_bf._base);

    fp->_flags &= ~(__SLBF | __SNBF | __SMBF);

    /*
     * Now put back whichever flag is needed, and fix _lbfsize
     * if line buffered.  Ensure output flush on exit if the
     * stream will be buffered at all.
     */

    switch (mode) 
	{
	case _IONBF:
		fp->_flags	|= __SNBF;
		fp->_bf._base	 = fp->_p = fp->_nbuf;
		fp->_bf._size	 = 1;
		break;

	case _IOLBF:
		fp->_flags 	|= __SLBF;
		fp->_lbfsize	 = -size;
		/* FALLTHROUGH */

	case _IOFBF:
		/* no flag */
		fp->_bf._base = fp->_p = (unsigned char *)buf;
		fp->_bf._size = size;
		break;
	default:
		break;
	}

    /*
     * Patch up write count if necessary.
     */

    if (fp->_flags & __SWR)
	fp->_w = (fp->_flags & (__SLBF | __SNBF)) ? (0) : (size);

    return (0);
    }
/* stdioLib.c - standard I/O library */

/* Copyright 1984-1995 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
02i,11feb95,jdi  doc tweak.
02h,05mar93,jdi  documentation cleanup for 5.1.
02g,13nov92,dnw  added __std{in,out,err} (SPR #1770)
		 made stdInitStd() be LOCAL.
		 changed stdioFp() create FILE if it doesn't already exist.
02f,20sep92,smb  documentation additions
02e,29jul92,smb  added stdioFp().
		 Modified the documentation for the new stdio library.
02d,29jul92,jcf  taken from stdioLib.c 
02c,26may92,rrr  the tree shuffle
02b,02apr92,jmm  added free() of memory if bad options passed to fdopen()
		 SPR # 1396
02a,27mar92,jmm  changed fopen() to free memory if the open() fails, SPR #1115
01z,25nov91,rrr  cleanup of some ansi warnings.
01y,12nov91,rrr  removed VARARG_OK, no longer needed with ansi c.
01x,07oct91,rrr  junk for r3000 braindamage.
01w,04oct91,rrr  passed through the ansification filter
                  -changed functions to ansi style
		  -changed includes to have absolute path from h/
		  -fixed #else and #endif
		  -changed VOID to void
		  -changed copyright notice
01v,18may91,gae  fixed varargs for 960 with conditional VARARG_OK,
		 namely: fscanf, fprintf, and scanf.
01u,04apr91,jdi  documentation cleanup; doc review by dnw.
01t,10aug90,dnw  added forward declaration of stdioExitStd ().
01s,08aug90,dnw  changed incorrect forward declaration for stdioCreateHook().
01r,10jun90,dnw  changed to call fioFormatV and fioScanV directly with
		   appropriate handling of varargs (removed doscan and doprnt)
		 moved all routine implementations of stdio macros to end of
		   module so that macros would be used in rest of code
		 spr 640: fprintf returns number of chars printed instead of OK
		 spr 754: vararg routines no longer limited to 16 args
		 fixed coercions to allow void to be defined as void one day.
01q,26jun90,jcf  lint.
01p,12mar90,jcf  changed std{in,out,err} to macros to fps in tcbx.
01o,16feb90,dab  fixed bug in doscan() that wouldn't match characters between
		 the input stream and the format specification.
01n,03aug89,dab  removed call to creat() in fopen().
01m,08apr89,dnw  changed stdioInit() to call taskVarInit().
01l,23mar89,dab  changed numerical constants to appropriate defines.
		 fixed fseek() to return only OK or ERROR.
01k,15nov88,dnw  documentation touchup.
01j,23sep88,gae  documentation touchup.
01i,13sep88,gae  removed ifdef'd sprintf which got into documentation.
01h,06sep88,gae  adjusted some argument declarations to please f2cgen.
01g,20aug88,gae  documentation.
01f,07jul88,jcf  changed malloc to match new declaration.
01e,29jun88,gae  documentation.  Added error messages in stioInit().
01d,22jun88,dnw  name tweaks.
01c,30may88,dnw  changed to v4 names.
01b,28may88,dnw  removed routines that had been excluded with "#if FALSE".
		 made stdioFlushBuf LOCAL.
		 cleaned up stdio{Init,Exit}Task; improved error msgs.
01a,28mar88,gae  created.
*/

/*
DESCRIPTION
This library provides a complete UNIX compatible standard I/O buffering
scheme.  It is beyond the scope of this manual entry to describe all aspects
of the buffering -- see the
.I "VxWorks Programmer's Guide:  I/O System"
and the Kernighan & Ritchie C manual.  This manual entry primarily highlights
the differences between the UNIX and VxWorks standard I/O.

FILE POINTERS
The routine fopen() creates a file pointer.  Use of the file pointer follows
conventional UNIX usage.  In a shared address space, however, and perhaps more
critically, with the VxWorks system symbol table, tasks may not use each
others' file pointers, at least not without some interlocking mechanism.  If it
is necessary to use the same name for a file pointer but have incarnations for
each task, then use task variables; see the manual entry for taskVarLib.

FIOLIB
Several routines normally considered part of standard I/O -- printf(),
sscanf(), and sprintf() -- are not implemented in stdio; they are
instead implemented in fioLib.  They do not use the standard I/O buffering
scheme.  They are self-contained, formatted, but unbuffered I/O
functions.  This allows a limited amount of formatted I/O to be achieved
without the overhead of the stdio library.

TASK TERMINATION
When a task exits, unlike in UNIX, it is the responsibility of the task to
fclose() its file pointers, except `stdin', `stdout', and `stderr'.  If a
task is to be terminated asynchronously, use kill() and arrange for a
signal handler to clean up.

INCLUDE FILES
stdio.h, taskLib.h

All the macros defined in stdio.h are also implemented as real functions so
that they are available from the VxWorks shell.

SEE ALSO
fioLib, ioLib, taskVarLib, sigLib, Kernighan & Ritchie C manual,
.pG "I/O System"

*/

#include "vxWorks.h"
#include "stdio.h"
#include "sys/types.h"
#include "ctype.h"
#include "ioLib.h"
#include "stdlib.h"
#include "taskLib.h"
#include "taskHookLib.h"
#include "stdarg.h"
#include "logLib.h"
#include "fcntl.h"
#include "unistd.h"
#include "errnoLib.h"
#include "string.h"
#include "fioLib.h"
#include "classLib.h"
#include "private/objLibP.h"
#include "private/stdioP.h"
#include "private/funcBindP.h"

/* locals */

LOCAL OBJ_CLASS fpClass;			/* file object class */
LOCAL BOOL	stdioInitialized = FALSE;

/* global variables */

CLASS_ID fpClassId = &fpClass;			/* file class id */


/*******************************************************************************
*
* stdioInit - initialize standard I/O support
*
* This routine installs standard I/O support.  It must be called before
* using `stdio' buffering.  If INCLUDE_STDIO is defined in configAll.h, it
* is called automatically by the root task usrRoot() in usrConfig.c.
*
* RETURNS:
* OK, or ERROR if the standard I/O facilities cannot be installed.
*/

STATUS stdioInit (void)
    {
    if ((!stdioInitialized) &&
	(classInit (fpClassId, sizeof (FILE), OFFSET (FILE, objCore),
		    (FUNCPTR) NULL, (FUNCPTR) NULL, (FUNCPTR) NULL) == OK))
	{
	_func_fclose	 = fclose;	/* attach fclose vfunc to taskLib */
	stdioInitialized = TRUE;	/* we've finished the initialization */
	}

    return (OK);
    }

/*******************************************************************************
*
* stdioFpCreate - allocate a new FILE structure
*
* RETURNS:
* The pointer to newly created file, or NULL if out of memory.
*
* NOMANUAL
*/

FILE *stdioFpCreate (void)
    {
    FAST FILE *fp = NULL;

    if ((stdioInit () == OK) &&
        ((fp = (FILE *)objAlloc (fpClassId)) != NULL))
	{
	fp->_p		= NULL;			/* no current pointer */
	fp->_r		= 0;
	fp->_w		= 0;			/* nothing to read or write */
	fp->_flags	= 1;			/* caller sets real flags */
	fp->_file	= -1;			/* no file */
	fp->_bf._base	= NULL;			/* no buffer */
	fp->_bf._size	= 0;
	fp->_lbfsize	= 0;			/* not line buffered */
	fp->_ub._base	= NULL;			/* no ungetc buffer */
	fp->_ub._size	= 0;
	fp->_lb._base	= NULL;			/* no line buffer */
	fp->_lb._size	= 0;
	fp->_blksize	= 0;
	fp->_offset	= 0;
	fp->taskId	= (int) taskIdCurrent;	/* task id might be useful */

	objCoreInit (&fp->objCore, fpClassId);	/* validate file object */
	}

    return (fp);
    }

/*******************************************************************************
*
* stdioFpDestroy - destroy and reclaim resources of specified file pointer
*
* RETURNS:
* OK, or ERROR if file pointer could not be destroyed.
*
* NOMANUAL
*/

STATUS stdioFpDestroy
    (
    FILE *fp
    )
    {
    /* fclose() deallocates any buffers associated with the file pointer */

    objCoreTerminate (&fp->objCore);		/* invalidate file pointer */

    return (objFree (fpClassId, (char *) fp));	/* deallocate file pointer */
    }

/*******************************************************************************
*
* stdioInitStd - initialize use of a standard file
*/

LOCAL STATUS stdioInitStd
    (
    int stdFd		/* standard file descriptor to initialize (0,1,2) */
    )
    {
    FILE *fp;

    if ((fp = stdioFpCreate ()) == NULL)
	return (ERROR);

    switch (stdFd)
	{
	case STD_IN:  fp->_flags = __SRD;    break;	/* read only */
	case STD_OUT: fp->_flags = __SWR;    break;	/* write only */
	case STD_ERR: fp->_flags = __SWRNBF; break;	/* write only unbuf'd */
	}

    fp->_file = stdFd;				/* standard fd */

    taskIdCurrent->taskStdFp[stdFd] = fp;	/* init private file pointer */

    return (OK);
    }

/******************************************************************************
*
* stdioFp - return the standard input/output/error FILE of the current task
* 
* This routine returns the specified standard FILE structure address of the
* current task.  It is provided primarily to give access to standard input,
* standard output, and standard error from the shell, where the usual
* `stdin', `stdout', `stderr' macros cannot be used.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS: The standard FILE structure address of the specified file
* descriptor, for the current task.
*/

FILE * stdioFp 
    (
    int stdFd		/* fd of standard FILE to return (0,1,2) */
    )
    {
    if (taskIdCurrent->taskStdFp [stdFd] == NULL)
	stdioInitStd (stdFd);

    return (taskIdCurrent->taskStdFp [stdFd]);
    }

/*******************************************************************************
*
* __stdin - get pointer to current task's stdin
*
* This function returns a pointer to the current task's stdin.  If the
* current task does not have a stdin then one is created.
*
* NOMANUAL
*/

FILE ** __stdin (void)
    {
    if (taskIdCurrent->taskStdFp [STD_IN] == NULL)
	stdioInitStd (STD_IN);

    return (&taskIdCurrent->taskStdFp [STD_IN]);
    }

/*******************************************************************************
*
* __stdout - get pointer to current task's stdout
*
* This function returns a pointer to the current task's stdout.  If the
* current task does not have a stdout then one is created.
*
* NOMANUAL
*/

FILE ** __stdout (void)
    {
    if (taskIdCurrent->taskStdFp [STD_OUT] == NULL)
	stdioInitStd (STD_OUT);

    return (&taskIdCurrent->taskStdFp [STD_OUT]);
    }

/*******************************************************************************
*
* __stderr - get pointer to current task's stderr
*
* This function returns a pointer to the current task's stderr.  If the
* current task does not have a stderr then one is created.
*
* NOMANUAL
*/

FILE ** __stderr (void)
    {
    if (taskIdCurrent->taskStdFp [STD_ERR] == NULL)
	stdioInitStd (STD_ERR);

    return (&taskIdCurrent->taskStdFp [STD_ERR]);
    }

/* stdioShow.c - standard I/O show library */

/* Copyright 1984-1993 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
01f,17mar99,jdi  doc: updated w/ info about proj facility (SPR 25727).
01e,16sep93,jmm  cleaned up warnings about printf args
01d,15sep93,kdl  fixed man page for stdioShowInit() (SPR #2244).
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  created.
*/

/*
DESCRIPTION
This library provides a show routine for file pointers.

NOMANUAL
*/


#include "vxWorks.h"
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include "taskLib.h"
#include "fioLib.h"
#include "classLib.h"
#include "errnoLib.h"
#include "private/objLibP.h"
#include "private/stdioP.h"


/******************************************************************************
*
* stdioShowInit - initialize the standard I/O show facility
*
* This routine links the file pointer show routine into the VxWorks system.
* It is called automatically when this show facility is
* configured into VxWorks using either of the following methods:
* .iP
* If you use the configuration header files, define
* INCLUDE_SHOW_ROUTINES in config.h.
* .iP
* If you use the Tornado project facility, select INCLUDE_STDIO_SHOW.
*
* RETURNS: OK, or ERROR if an error occurs installing the file pointer show
* routine.
*/

STATUS stdioShowInit (void)
    {
    classShowConnect (fpClassId, (FUNCPTR)stdioShow);

    return (OK);
    }
/*******************************************************************************
*
* stdioShow - display file pointer internals
*
* This routine displays information about a specified stream.
*
* RETURNS: OK, or ERROR if the file pointer is invalid.
*/

STATUS stdioShow
    (
    FAST FILE * fp,	/* stream */
    int		level	/* level */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (ERROR);				/* invalid file pointer */

    printf ("%-20s: %#-10x\n", "Owning Task", fp->taskId);
    printf ("%-20s: %-10hd\n", "File Descriptor", fp->_file);
    printf ("%-20s: %#-10x\n", "Current Position", (unsigned int) fp->_p);
    printf ("%-20s: %#-10x\n", "Read Space Left", fp->_r);
    printf ("%-20s: %#-10x\n", "Write Space Left", fp->_w);
    printf ("%-20s: %#-10x\n", "Buffer Base", (unsigned int) fp->_bf._base);
    printf ("%-20s: %#-10x\n", "Buffer Size", fp->_bf._size);	
    printf ("%-20s: %#-10x\n", "Ungetc Buffer Base",
	    (unsigned int) fp->_ub._base);
    printf ("%-20s: %#-10x\n", "Ungetc Buffer Size", fp->_ub._size);	
    printf ("%-20s: %#-10x\n", "Line Buffer Base", (unsigned int) fp->_lb._base);
    printf ("%-20s: %#-10x\n", "Line Buffer Size", fp->_lb._size);	
    printf ("%-20s: %#-10x\n", "stat.st_blksize", fp->_blksize);	
    printf ("%-20s: %#-10x\n", "lseek Offset", fp->_offset);	
    printf ("%-20s: %#-10hx\n", "Flags", fp->_flags);	

    /* someday display the buffer contents with level >= 1 */

    return (OK);
    }
/* tmpfile.c - creat a temporary file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/* 
modification history 
-------------------- 
01d,05mar93,jdi  documentation cleanup for 5.1.
01c,21sep92,smb  tweaks for mg.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  written.
*/ 

/* 
DESCRIPTION 
 
INCLUDE FILE: stdio.h, string.h 
 
SEE ALSO: American National Standard X3.159-1989 

NOMANUAL
*/ 

#include "vxWorks.h"
#include "stdio.h"

/*******************************************************************************
*
* tmpfile - create a temporary binary file (Unimplemented) (ANSI)
*
* This routine is not be implemented
* because VxWorks does not close all open files at task exit.
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: NULL 
*/

FILE * tmpfile (void)
    {
    return (NULL);
    }
/* tmpnam.c	- devise a temporary name. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/* 
modification history 
--------------------
01d,23sep93,jmm  made tmpnam()'s buffer static (spr 2525)
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,smb  written.
*/ 

/*
DESCRIPTION

INCLUDE FILE: stdio.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "string.h"

/******************************************************************************
*
* tmpnam - generate a temporary file name (ANSI)
*
* This routine generates a string that is a valid file name and not the same
* as the name of an existing file.  It generates a different string each
* time it is called, up to TMP_MAX times.
*
* If the argument is a null pointer, tmpnam() leaves its
* result in an internal static object and returns a pointer to that
* object.  Subsequent calls to tmpnam() may modify the same
* object.  If the argument is not a null pointer, it is assumed to
* point to an array of at least L_tmpnam chars; tmpnam() writes
* its result in that array and returns the argument as its value.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS: A pointer to the file name.
*/

char * tmpnam
    (
    char *  s	/* name buffer */
    )
    {
    int             index;
    char *          pos;
    ushort_t        t;
    static char     buf [L_tmpnam];	/* internal buffer for name */
    static ushort_t seed = 0;		/* used to generate unique name */

    /* if parameter is NULL use internal buffer */
    if (s == NULL)
    	s = buf;

    /* generate unique name */

    strcpy (s, "tmp");

    /* fill up the name buffer from the last position */
    index = 5;
    pos = s + strlen (s) + index;
    *pos = '\0';

    seed++;
    for (t = seed; 0 <= --index; t >>= 3)
    	*--pos = '0' + (t & 07);

    /* return name buffer */
    return (s);
    }
/* ungetc.c - return a character to a stream. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdlib.h, string.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "objLib.h"
#include "private/stdioP.h"


/******************************************************************************
*
* __submore - internal routine
*
* Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
* the buffer moves, so that it points the same distance from the end,
* and move the bytes in the buffer around as necessary so that they
* are all at the end (stack-style).
* 
* INCLUDE FILES: stdio.h 
*
* RETURNS: 
* NOMANUAL
*/

static int __submore
    (
    FAST FILE *fp
    )
    {
    FAST int       i;
    FAST uchar_t * p;

    if (fp->_ub._base == fp->_ubuf) 
	{
	/* Get a new buffer (rather than expanding the old one) */

	if ((p = malloc ((size_t)BUFSIZ)) == NULL)
	    return (EOF);

	fp->_ub._base = p;
	fp->_ub._size = BUFSIZ;

	p += BUFSIZ - sizeof(fp->_ubuf);

	for (i = sizeof(fp->_ubuf); --i >= 0;)
	    p[i] = fp->_ubuf[i];

	fp->_p = p;

	return (0);
	}

    i = fp->_ub._size;

    p = realloc (fp->_ub._base, i << 1);

    if (p == NULL)
	return (EOF);

    (void) bcopy ((void *)p, (void *)(p + i), (size_t)i);

    fp->_p	  = p + i;
    fp->_ub._base = p;
    fp->_ub._size = i << 1;

    return (0);
    }

/******************************************************************************
*
* ungetc - push a character back into an input stream (ANSI)
*
* This routine pushes a character <c> (converted to an `unsigned char') back
* into the specified input stream.  The pushed-back characters will be
* returned by subsequent reads on that stream in the reverse order of their
* pushing.  A successful intervening call on the stream to a file
* positioning function (fseek(), fsetpos(), or rewind()) discards any
* pushed-back characters for the stream.  The external storage corresponding
* to the stream is unchanged.
*
* One character of push-back is guaranteed.  If ungetc() is called too many
* times on the same stream without an intervening read or file positioning
* operation, the operation may fail.
*
* If the value of <c> equals EOF, the operation fails and the 
* input stream is unchanged.
*
* A successful call to ungetc() clears the end-of-file indicator for the
* stream.  The value of the file position indicator for the stream after
* reading or discarding all pushed-back characters is the same as it was
* before the character were pushed back.  For a text stream, the value of
* its file position indicator after a successful call to ungetc() is
* unspecified until all pushed-back characters are read or discarded.
* For a binary stream, the file position indicator is decremented by each
* successful call to ungetc(); if its value was zero before a call, it is
* indeterminate after the call.
* 
* INCLUDE: stdio.h 
*
* RETURNS:
* The pushed-back character after conversion, or EOF if the operation fails.
*
* SEE ALSO: getc(), fgetc()
*/

int ungetc
    (
    int		 c,	/* character to push */
    FAST FILE *	 fp	/* input stream */
    )
    {
    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    if (c == EOF)
	return (EOF);

    if ((fp->_flags & __SRD) == 0) 
	{
	/*
	 * Not already reading: no good unless reading-and-writing.
	 * Otherwise, flush any current write stuff.
	 */

	if ((fp->_flags & __SRW) == 0)
	    return (EOF);

	if (fp->_flags & __SWR) 
	    {
	    if (__sflush(fp))
		return (EOF);

	    fp->_flags  &= ~__SWR;
	    fp->_w	 = 0;
	    fp->_lbfsize = 0;
	    }
	fp->_flags |= __SRD;
	}

    c = (uchar_t) c;

    /*
     * If we are in the middle of ungetc'ing, just continue.
     * This may require expanding the current ungetc buffer.
     */

    if (HASUB(fp)) 
	{
	if (fp->_r >= fp->_ub._size && __submore(fp))
	    return (EOF);

	*--fp->_p = c;
	fp->_r++;

	return (c);
	}

    /*
     * If we can handle this by simply backing up, do so,
     * but never replace the original character.
     * (This makes sscanf() work when scanning `const' data.)
     */

    if ((fp->_bf._base != NULL) && 
	(fp->_p > fp->_bf._base) &&
	(fp->_p[-1] == c)) 
	{
	fp->_p--;
	fp->_r++;

	return (c);
	}

    /*
     * Create an ungetc buffer.
     * Initially, we will use the `reserve' buffer.
     */

    fp->_ur				= fp->_r;
    fp->_up				= fp->_p;
    fp->_ub._base			= fp->_ubuf;
    fp->_ub._size			= sizeof(fp->_ubuf);
    fp->_ubuf[sizeof(fp->_ubuf) - 1]	= c;
    fp->_p				= &fp->_ubuf[sizeof(fp->_ubuf) - 1];
    fp->_r				= 1;

    return (c);
    }
/* vfprintf.c - print to a file. stdio.h */

/* Copyright 1992-1993 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,05mar93,jdi  documentation cleanup for 5.1.
01b,20sep92,smb  documentation additions
01a,29jul92,jcf  Added OBJ_VERIFY, use fioFormatV()
	   +smb  taken from UCB stdio
*/

/*
DESCRIPTION
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.

INCLUDE FILE: stdio.h, stdarg.h

SEE ALSO: American National Standard X3.159-1989

NOMANUAL
*/

#include "vxWorks.h"
#include "stdarg.h"
#include "fioLib.h"
#include "objLib.h"
#include "private/stdioP.h"

/* forward declarations */

LOCAL STATUS putbuf (char *buffer, int nbytes, FILE *fp);


/*******************************************************************************
*
* vfprintf - write a formatted string to a stream (ANSI)
*
* This routine is equivalent to fprintf(), except that it takes the variable
* arguments to be formatted from a list <vaList> of type `va_list' rather
* than from in-line arguments.
*
* INCLUDE FILES: stdio.h 
*
* RETURNS:
* The number of characters written, or a negative value if an
* output error occurs.
*
* SEE ALSO: fprintf()
*/

int vfprintf
    (
    FILE *	  fp,		/* stream to write to */
    const char *  fmt,		/* format string */
    va_list       vaList	/* arguments to format string */
    )
    {
    uchar_t localbuf[BUFSIZE];
    int	    ret;
    FILE    fake;

    if (OBJ_VERIFY (fp, fpClassId) != OK)
	return (EOF);

    if (cantwrite (fp))
	return (EOF);

    if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && fp->_file >= 0)
	{
	/* 
	 * This optimizes an fprintf to unbuffered unix file by creating a
	 * temporary buffer.  This avoids worries about ungetc buffers and
	 * so forth.  It is particularly useful for stderr.
	 */

	fake._flags 	= fp->_flags & ~__SNBF;		/* turn on buffering */
	fake._file  	= fp->_file;			/* store fd */
	fake._bf._base	= fake._p = localbuf;		/* set up the buffer */
	fake._bf._size	= fake._w = sizeof(localbuf);	/* initialize size */
	fake._lbfsize	= 0;				/* init to be safe */
	fake._r		= 0;				/* init to be safe */
	fake._ub._base	= NULL;				/* init to be safe */
	fake._ub._size	= 0;				/* init to be safe */
	fake._lb._base	= NULL;				/* init to be safe */
	fake._lb._size	= 0;				/* init to be safe */
	objCoreInit (&fake.objCore, fpClassId);		/* validate fake fp */

        ret = fioFormatV (fmt, vaList, putbuf, (int) &fake);

	if ((ret >= 0) && fflush (&fake))
	    ret = EOF;

	if (fake._flags & __SERR)
	    fp->_flags |= __SERR;

	objCoreTerminate (&fake.objCore);		/* invalidate fp */
	}
    else
	{
	ret = fioFormatV (fmt, vaList, putbuf, (int) fp);
    	ret = (ferror (fp)) ? EOF : ret;
        }

    return (ret);
    }

/*******************************************************************************
*
* putbuf - put a buffer on an output stream
* NOMANUAL
*/

LOCAL STATUS putbuf
    (
    char * buffer,       /* source */
    int    nbytes,       /* number of bytes in source */
    FILE * fp            /* stream */
    )
    {
    FAST int ix;

    for (ix = 0; ix < nbytes; ix++)
	putc (buffer [ix], fp);

    return (OK);
    }
